package com.flower8.service.impl;

import com.flower8.dao.CrowdfundDao;
import com.flower8.dao.MessageDao;
import com.flower8.dao.UserDao;
import com.flower8.dto.ActiveCrowdfundDto;
import com.flower8.dto.ActiveOfferDto;
import com.flower8.model.CrowdfundPay;
import com.flower8.model.SysMenu;
import com.flower8.model.User;
import com.flower8.service.CrowdfundService;
import com.flower8.sysconfig.constants.SysConstants;
import com.flower8.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 众筹service实现
 * @Author: tanglin
 * @Date: 2019/09/19 15:31
 * @Version: 1.0
 */
@Service
public class CrowdfundServiceImpl implements CrowdfundService {

    @Autowired
    private CrowdfundDao crowdfundDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private MessageDao messageDao;

    /**
     * 修改众筹单状态
     * @param pd
     * @throws Exception
     */
    @Override
    @Transactional
    public void editCrowdfundStatus(PageData pd) throws Exception {
        crowdfundDao.editCrowdfundStatus(pd);
    }

    /**
     * 修改用户的信用分
     * @param pd
     * @throws Exception
     */
    @Override
    @Transactional
    public void editCreditScoreByUserId(PageData pd) throws Exception {
        //修改众筹单状态
        crowdfundDao.editCrowdfundStatus(pd);
        //1、查询该用户的总信用分
        User user = userDao.getUserById(Long.parseLong(pd.get("userId").toString()));
        //计算总信用分
        long totalScore = user.getCreditPoint()+Long.parseLong(pd.getString("score"));
        pd.put("totalCreditScore",totalScore);
        //2、修改用户表中的总信用分
        userDao.updateTotalCreditPoint(pd);
        //3、向信用分记录表中添加一条记录
        crowdfundDao.addCreditScore(pd);
        //4、发送一条扣分消息
        Map msgParam = new HashMap();
        msgParam.put("uid", pd.get("userId").toString());
        msgParam.put("type", 1);
        msgParam.put("category", 3);
        msgParam.put("icon", SysConstants.MSG1);
        String msg = "因" + pd.get("reason").toString() + "被扣除信用分" + (-Integer.parseInt(pd.get("score").toString()))+"分" ;
        msgParam.put("contents", msg);

        messageDao.addMsg(msgParam);
    }

    /**
     * 查询所有用户众筹列表
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<ActiveCrowdfundDto> getCrowdfundList(PageData pd) throws Exception {
        return crowdfundDao.getCrowdfundList(pd);
    }

    /**
     * 查询所有用户众筹列表数量
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getCrowdfundListCount(PageData pd) throws Exception {
        return crowdfundDao.getCrowdfundListCount(pd);
    }

    /**
     * 查询众筹单筹款详情
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<ActiveOfferDto> getCrowdfundDetailList(PageData pd) throws Exception {
        return crowdfundDao.getCrowdfundDetailList(pd);
    }

    /**
     * 查询众筹单筹款详情数量
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getCrowdfundDetailListCount(PageData pd) throws Exception {
        return crowdfundDao.getCrowdfundDetailListCount(pd);
    }

    /**
     * 根据众筹单id查询
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public ActiveCrowdfundDto getCrowdfundById(Long id) throws Exception {
        return crowdfundDao.getCrowdfundById(id);
    }

    /**
     * 修改众筹单
     * @param pd
     * @throws Exception
     */
    @Override
    @Transactional
    public void updCrowd(PageData pd) throws Exception {
        crowdfundDao.updCrowd(pd);
        long id = Long.valueOf(pd.get("cid").toString());
        ActiveCrowdfundDto activeCrowdfundDto = crowdfundDao.getCrowdfundById(id);
        long flag = activeCrowdfundDto.getFlag();
        if (flag >= 3) {
            PageData pageData = new PageData();
            pageData.put("cid", id);
            pageData.put("status", 6);
            crowdfundDao.updCrowd(pageData);
        }
        
    }

    /**
     * 打款记录查询列表
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<CrowdfundPay> tranferMoneyList(PageData pd) throws Exception {
        return crowdfundDao.tranferMoneyList(pd);
    }

    /**
     * 打款记录查询列表数量
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int tranferMoneyCount(PageData pd) throws Exception {
        return crowdfundDao.tranferMoneyCount(pd);
    }

    /**
     * 查询打款记录
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> tranferMoneyDetail(PageData pd) throws Exception {
        return crowdfundDao.tranferMoneyDetail(pd);
    }

    /**
     * 查询打款记录数量
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int tranferMoneyDetailCount(PageData pd) throws Exception {
        return crowdfundDao.tranferMoneyDetailCount(pd);
    }


}
